package com.lwh.yygh.hosp.controller.api;

import com.lwh.yygh.model.hosp.Schedule;
import com.lwh.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.Page;
import com.lwh.yygh.common.exception.YyghException;
import com.lwh.yygh.common.helper.HttpRequestHelper;
import com.lwh.yygh.common.result.Result;
import com.lwh.yygh.common.result.ResultCodeEnum;
import com.lwh.yygh.common.utlis.MD5;
import com.lwh.yygh.hosp.service.DepartmentService;
import com.lwh.yygh.hosp.service.HospitalApiService;
import com.lwh.yygh.hosp.service.HospitalService;
import com.lwh.yygh.hosp.service.ScheduleService;
import com.lwh.yygh.model.hosp.Department;
import com.lwh.yygh.model.hosp.Hospital;
import com.lwh.yygh.vo.hosp.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author lwh
 * @date 2021/5/21 20:00
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalApiService hospitalApiService;

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);
        //1.获取医院系统传过来的密钥，密钥进行过MD5加密
        String sign = (String) map.get("sign");

        //2.根据传递过来的医院编号，查询数据库，查看签名是否一样
        String hoscode = (String) map.get("hoscode");
        String singKey = hospitalService.getSignKey(hoscode);

        //3.把数据库查询出来的数据进行MD5加密然后比较
        String singKeyMD5 = MD5.encrypt(singKey);
        if (!singKeyMD5.equals(sign)) {
            hospitalApiService.save(map);
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //我们图片是通过base64的方式来传输，传输过程会把符号+或者空
        String logoData = (String) map.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        map.put("logoData",logoData);
        return Result.ok();
    }


    //医院查询接口
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);
        //获取传递过来的医院编号
        String hoscode = (String) map.get("hoscode");
        //1.获取医院系统传过来的密钥，密钥进行过MD5加密
        String sign = (String) map.get("sign");

        //2.根据传递过来的医院编号，查询数据库，查看签名是否一样
        String singKey = hospitalService.getSignKey(hoscode);

        //3.把数据库查询出来的数据进行MD5加密然后比较
        String singKeyMD5 = MD5.encrypt(singKey);
        if (!singKeyMD5.equals(sign)) {
            hospitalApiService.save(map);
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }


    //上传课室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);
        //获取传递过来的医院编号
        String hoscode = (String) map.get("hoscode");
        //1.获取医院系统传过来的密钥，密钥进行过MD5加密
        String sign = (String) map.get("sign");

        //2.根据传递过来的医院编号，查询数据库，查看签名是否一样
        String singKey = hospitalService.getSignKey(hoscode);

        //3.把数据库查询出来的数据进行MD5加密然后比较
        String singKeyMD5 = MD5.encrypt(singKey);
        if (!singKeyMD5.equals(sign)) {
            hospitalApiService.save(map);
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service
        departmentService.save(map);
        return Result.ok();
    }


    //查询课室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);

        //获取医院的编号
        String hoscode = (String) map.get("hoscode");

        //当前页和每页记录数
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String) map.get("limit"));

        DepartmentQueryVo queryVo = new DepartmentQueryVo();
        queryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,queryVo);
        return Result.ok(pageModel);
    }


    //上传排班
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);

        //TODO 签名校验
        scheduleService.save(map);
        return Result.ok();
    }


    //删除科室,根据医院和课室编号进行删除
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");

        //签名的校验
        //1.获取医院系统传过来的密钥，密钥进行过MD5加密
        String sign = (String) map.get("sign");

        //2.根据传递过来的医院编号，查询数据库，查看签名是否一样
        String singKey = hospitalService.getSignKey(hoscode);

        //3.把数据库查询出来的数据进行MD5加密然后比较
        String singKeyMD5 = MD5.encrypt(singKey);
        if (!singKeyMD5.equals(sign)) {
            hospitalApiService.save(map);
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //进行删除
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //查询排班
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);

        //获取医院的编号
        String hoscode = (String) map.get("hoscode");
        //获取科室编号
        String depcode = (String) map.get("depcode");

        //当前页和每页记录数
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String) map.get("limit"));
        ScheduleQueryVo queryVo = new ScheduleQueryVo();
        queryVo.setHoscode(hoscode);

        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,queryVo);
        return Result.ok(pageModel);
    }


    //删除排班
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号和排班编号
        String hoscode = (String) map.get("hoscode");
        String hosScheduleId = (String) map.get("hosScheduleId");

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

}
